import { reactive, ref } from 'vue'
// 导入串口第三方库
const { SerialPort } = window.require('serialport')
import { useMessage } from '@renderer/hooks/message'
const messageTip = useMessage()
import { useOfflineBurningMode } from '@renderer/hooks/offlineBurningMode'
const offlineBurningModeHook = useOfflineBurningMode()

interface portInterface {
    open: Function
    close: Function
    on: Function
    write: Function
}

export function usePortsHooks() {
    let portsStatus = ref<boolean>(false)
    // 比特率
    const currentBaud = 115200
    // 数据位
    const currentDataBit = 8
    // 校验位
    const currentVaild = 'none'
    // 串口列表
    let serialPortList = ref([])
    // 当前串口
    const currentSerialPort = ref('')
    let port = null

    // 获取串口列表
    const getSerialPorts = async () => {
        serialPortList.value = await SerialPort.list()
        console.log('serialPortList', serialPortList)
    }

    const handleSerialPort = async () => {
        // 获取选中的串口
        if (portsStatus.value) {
            port && port?.close()
            portsStatus.value = false;
        } else {
            // 连接串口,配置对应的数据
            port = new SerialPort({
                // 连接的串口信息数据
                // ...serialPortItem,
                path: currentSerialPort.value,
                // 比特率
                baudRate: currentBaud,
                // 数据位
                dataBits: currentDataBit,
                // 校验位
                parity: currentVaild
            });

            console.log('this.port', port)

            await port?.open()
            portsStatus.value = true;

            port?.on('data', (data) => {
                console.log('接收数据111', data)

                checkReceiveData(data)
            })

            port?.on('error', (err) => {
                console.log('err', err)
                // 串口正在打开错误不提示
                if (err.toString() === 'Error: Port is opening') return
                portsStatus.value = false;
                messageTip.error(err);
            })

            port.on('close', () => {
                console.log('串口已断开');
                portsStatus.value = false;
            })
        }
    }

    // 校验命令字
    const checkReceiveData = (childCode) => {
        switch (childCode[1]) {
            case 0xA0: // Unimaster版本
                break;
            case 0x16: //  清空升级缓冲区
                offlineBurningModeHook.clearUpgradTypeFn(childCode);
                break;
            case 0x30: // 离线烧录 （配置）
                offlineBurningModeHook.upgradOfflineFileFn(childCode, 3, 8, childCode[1])
                break;
            case 0x31: // 离线烧录 （ui）
                offlineBurningModeHook.upgradOfflineFileFn(childCode, 2, 4, childCode[1])
                break;
            case 0x32: // 离线烧录 （boot）
                offlineBurningModeHook.upgradOfflineFileFn(childCode, 0, 1, childCode[1])
                break;
            case 0x33: // 离线烧录 （app）
                offlineBurningModeHook.upgradOfflineFileFn(childCode, 1, 2, childCode[1])
                break;
            case 0x34: // 升级项结束写入
                offlineBurningModeHook.getUpgradEndFn(childCode, childCode[1]);
                break;
        }
    }


    return {
        portsStatus,
        serialPortList,
        currentSerialPort,
        getSerialPorts,
        handleSerialPort
    }
}